module io_mod; 
use test_mod;
implicit none;
character(LEN=1), parameter :: FORTRAN_COMMENT = '!';   


contains 

logical function f95_input_comment(str) result(res);  implicit none;
  character(LEN=*), intent(in) :: str;   
  res = (str(1:1) == FORTRAN_COMMENT);
  if (res)  call dbg_str("f95 comment=", TRIM(str));
  return;
end function
SUBROUTINE f95_ignore_comments(nin);    implicit none;  
  integer, intent(in) :: nin;   
  character(LEN=1024) :: buff;   
  do while (.TRUE.)
    read(nin, *) buff; 
    buff = ADJUSTL(buff); 
    if (.NOT. f95_input_comment(buff)) exit; ! exit loop
  end do  
  backspace(unit=nin);
end SUBROUTINE

SUBROUTINE read_str(nin, info, res);    implicit none;  
  integer, intent(in) :: nin;   
  character(LEN=*), intent(in) :: info;   
  character(LEN=*), intent(out) :: res;   
  character(LEN=1024) :: buff;   
  call dbg_str("-->read_str(info=", info);  
  call f95_ignore_comments(nin);
  read(nin, *) buff, res; 
  !call dbg_str("buff=", TRIM(buff));
  !call dbg_str("res=", TRIM(res));
  call assert_help_str(TRIM(info), TRIM(buff));
  call dbg_str(info, res);
  return;
end SUBROUTINE

SUBROUTINE read_int(nin, info, res);    implicit none;  
  integer, intent(in) :: nin;   
  integer, intent(out) :: res;   
  character(LEN=*), intent(in) :: info;   
  character(LEN=1024) :: buff;   
  call dbg_str("-->read_int(info=", info); 
  call f95_ignore_comments(nin);
  read(nin, *) buff, res;  
  call assert_help_str(info, buff);
  call dbg_int(info, res);
  return;
end SUBROUTINE

SUBROUTINE read_real(nin, info, res);    implicit none;  
  integer, intent(in) :: nin;   
  character(LEN=*), intent(in) :: info;  
  real(R14), intent(out) ::  res;
  character(LEN=1024) :: buff;   
  call dbg_str("-->read_real(info=", info); 
  call f95_ignore_comments(nin);
  read(nin, *) buff, res;  
  call assert_help_str(info, buff);
  call dbg_real(info, res);
  return;
end SUBROUTINE

end module 